<template>
  <div>
    <el-card class="box-card" style="width: 400px;">
      <div slot="header" class="clearfix">
        <span>{{ admin.name }}</span>
      </div>
      <div>
        <div style="display: flex;justify-content: center">
          <el-upload
              action="/admin/userface"
              :headers="headers"
              :data="admin"
              :on-success="onSuccess"
              :show-file-list="false"
              >
            <img src=../assets/驯龙高手-崔斯塔娜.jpg alt="" title="点击修改头像" style="height: 100px;width: 100px;
border-radius: 50px;">
          </el-upload>

        </div>
        <div>电话号码:
          <el-tag>{{ admin.phone }}</el-tag>
        </div>
        <div>联系地址:
          <el-tag>{{ admin.address }}</el-tag>
        </div>
        <div>
          用户权限:
          <el-tag type="success" v-for="(r,index) in admin.roles" :key="index"> {{ r.nameZh }}</el-tag>
        </div>
        <div style="display: flex;justify-content: space-around;margin-top: 10px">
          <el-button type="primary" @click="showUpdateAdminInfo">修改信息</el-button>
          <el-button type="danger" @click="showUpdatePassword">修改密码</el-button>
        </div>
      </div>
    </el-card>
    <el-dialog
        title="编辑用户信息"
        :visible.sync="dialogVisible"
        width="30%">
      <div>
        <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="用户名称">
            <el-input type="text" v-model="admin2.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="电话号码">
            <el-input type="text" v-model="admin2.phone" autocomplete="off" ></el-input>
          </el-form-item>
          <el-form-item label="联系地址">
            <el-input type="text" v-model="admin2.address" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="updateAdminInfo">确 定</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-dialog>

    <el-dialog
        title="修改密码"
        :visible.sync="passwordDialogVisible"
        width="30%">
      <div>
        <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="旧密码" prop="pass">
            <el-input type="password" v-model="ruleForm.oldPass" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="新密码" prop="pass">
            <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="确认密码" prop="checkPass">
            <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import store from "@/store";

export default {
  name: "adminInfo",
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass');
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      headers:{
        Authorization:window.sessionStorage.getItem('tokenStr')
      },
      admin: '',
      admin2: '',
      dialogVisible: false,
      passwordDialogVisible: false,
      ruleForm: {
        pass: '',
        checkPass: '',
        oldPass:''
      },
      rules: {
        pass: [
          {validator: validatePass, trigger: 'blur'}
        ],
        checkPass: [
          {validator: validatePass2, trigger: 'blur'}
        ],
        oldPass: [
          {validator: validatePass, trigger: 'blur'}
        ]
      }
    };
  },
  mounted() {
    this.initData();
  },
  methods: {
    onSuccess(){
      this.initData();
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          //获取当前用户id
          this.ruleForm.adminId = this.admin.id;
          this.putRequest('/admin/pass',this.ruleForm).then(resp =>{
            if (resp){
              //密码修改成功，重新登陆，清空缓存
              this.postRequest('/logout');
              window.sessionStorage.removeItem('user');
              window.sessionStorage.removeItem('tokenStr');
              //清空菜单
              this.$store.commit('initRoutes',[]);
              //回到登陆界面
              this.$router.replace('/')
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    showUpdatePassword() {
      this.passwordDialogVisible = true;
    },
    showUpdateAdminInfo() {
      this.dialogVisible = true;
    },
    initData() {
      this.getRequest('/admin/info').then(resp => {
        if (resp) {
          this.admin = resp;
          this.admin2 = Object.assign({}, this.admin);
          window.sessionStorage.setItem('user', JSON.stringify(resp));
          this.$store.commit('INIT_ADMIN', resp);
        }
      })
    },
    updateAdminInfo() {
      this.putRequest('/admin/info', this.admin2).then(res => {
        if (res) {
          this.dialogVisible = false;
          this.initData();
        }
      })
    }
  }
}
</script>

<style scoped>

</style>